<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/7/17
 */

namespace app\user\controller;


use app\educational\model\MessageModel;
use app\student\model\StudentModel;
use cmf\controller\UserBaseController;
use think\Db;
use think\Log;
use think\Request;

class FrontMessageController extends UserBaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * 消息中心
     * @return mixed
     */
    public function index()
    {
        $limit = config('limit_front_per_page');
        $msg_status = -1;//全部
        $param = $this->request->param();
        //当前家长
        $cur_uid = cmf_get_current_user_id();
        //获取家长的孩子
        $student_mdl = new StudentModel();
        $students = $student_mdl->getStudentsByPid($cur_uid, $this->front_shop_id);
        if(!$students){
            $this->redirect('jiaxiaotong/JiaXiaoTong/index');
        }

        if(isset($_SESSION['student_id']) && !empty($_SESSION['student_id'])){
            $student_id = $_SESSION['student_id'];
        }else{
            //设置当前学生
            $_SESSION['student_id'] = $students[0]['id'];
            //默认第一个孩子
            $student_id = $students[0]['id'];
        }

        $where['m.shop_id'] = ['=', $this->front_shop_id];
        $where['m.type'] = ['=', 0];//消息类型(0:站内信息,1:短信)
        $where['mu.receive_uid'] = ['=', $cur_uid];

        //是否已读(0:未读，1已读)
        if (isset($param['status']) && is_numeric($param['status'])) {
            switch (intval($param['status'])) {
                case 0:
                    $msg_status = 0;
                    break;
                case 1:
                    $msg_status = 1;
                    break;
                default:
                    $msg_status = -1;
                    break;
            }
            if ($msg_status != -1) {
                $where['mu.msg_status'] = ['=', $msg_status];
            }
        }
        //排序
        $sort = ['mu.msg_status' => 'asc'];
        //获取消息
        $msg_mdl = new MessageModel();
        $data = $msg_mdl->getMessages($where, $sort, $limit, []);

        $this->assign('student_id', $student_id);
        $this->assign('students', $students);
        $this->assign('msg_status', $msg_status);
        $this->assign('messages', $data);
        $this->assign('page', $data->render());

        return $this->fetch(':messageList');
    }

    /**
     *标记消息已读
     */
    public function ajaxStatus()
    {
        $param = $this->request->param();
        if (!isset($param['ids'])) {
            $json_data = [
                'code' => 0,
                'msg' => '参数不能为空',
                'data' => []
            ];
            return json($json_data);
        }
        $msg_user_ids = trim($param['ids'],',');
        //转换为数组
        $ids = explode(',',$msg_user_ids);
        if (count($ids) <= 0) {
            $json_data = [
                'code' => 0,
                'msg' => '参数不能为空',
                'data' => []
            ];
            return json($json_data);
        }

        //当前用户
        $cur_uid = cmf_get_current_user_id();
        //是否已读(0:未读，1已读)
        $msg_status = 1;
        if (count($ids) == 1) {
            //单个处理
            $result = Db::name('message_user')->where(['id' => $msg_user_ids, 'receive_uid' => $cur_uid])
                ->update(['msg_status' => $msg_status]);
        } else {
            //批量处理
            $result = Db::name('message_user')->where(['id' => ['in', $msg_user_ids], 'receive_uid' => $cur_uid])
                ->update(['msg_status' => $msg_status]);
        }

        if ($result) {
            $json_data = [
                'code' => 1,
                'msg' => '处理成功',
                'data' => ''
            ];
            return json($json_data);

        } else {
            $json_data = [
                'code' => 0,
                'msg' => '处理失败',
                'data' => []
            ];
            return json($json_data);
        }
    }

    /*
     * 删除
     */
    public function delete()
    {
        $param = $this->request->param();
        if (!isset($param['ids'])) {
            $json_data = [
                'code' => 0,
                'msg' => '参数不能为空',
                'data' => []
            ];
            return json($json_data);
        }
        $msg_user_ids = trim($param['ids'],',');
        //转换为数组
        $ids = explode(',',$msg_user_ids);
        if (count($ids) <= 0) {
            $json_data = [
                'code' => 0,
                'msg' => '参数不能为空',
                'data' => []
            ];
            return json($json_data);
        }

        //当前用户
        $cur_uid = cmf_get_current_user_id();
        if (count($ids) == 1) {
            //单个删除
            $result = Db::name('message_user')->where(['id' => $msg_user_ids, 'receive_uid' => $cur_uid])->update(['delete_time'=>time()]);
        } else {
            //批量删除
            $result = Db::name('message_user')->where(['id' => ['in', $msg_user_ids], 'receive_uid' => $cur_uid])->update(['delete_time'=>time()]);
        }

        if ($result) {
            $json_data = [
                'code' => 1,
                'msg' => '删除成功',
                'data' => ''
            ];
            return json($json_data);

        } else {
            $json_data = [
                'code' => 0,
                'msg' => '删除失败',
                'data' => ''
            ];
            return json($json_data);
        }

    }

}